package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.query.TravelListQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private ITravelService travelService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IStrategyCommentService strategyCommentService;

    /**
     * 注册
     * @param user 用户
     * @return json
     */
    @PostMapping
    public Object register(User user){
        JSONResult jsonResult = new JSONResult();
        try {
            userService.saveOrUpdate(user);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());

        }
        return jsonResult;
    }

    @PutMapping("{id}")
    public Object update(User user){
        userService.saveOrUpdate(user);
        JSONResult jsonResult = new JSONResult();
        jsonResult.setResult(user);
        return jsonResult;
    }



    @GetMapping("/{authorId}/travels")
    public Object list(TravelQueryObject qo){
        qo.setOrderBy("t.lastUpdateTime desc");
        return travelService.query(qo);
    }

    //关注查询
    @GetMapping("/relations/{id}")
    public Object queryRelation(@PathVariable("id") Long fansId,Long attentionId,String type){
        if("map".equals(type)){
            return userService.queryNum(fansId);
        }
        return userService.selectAttention(fansId,attentionId);
    }


    //关注
    @PostMapping("/relations")
    public Object attention(Long fansId,Long attentionId){
        userService.saveRelation(fansId,attentionId);
        return new JSONResult();
    }

    //取消关注
    @DeleteMapping("/relations/{id}")
    public Object cancelAttention(@PathVariable("id")Long fansId,Long attentionId){
        userService.deleteRelation(fansId,attentionId);
        return new JSONResult();
    }

    //某个人的评论
    @GetMapping("{userId}/strategyComments")
    public Object listStrategyCommentById(StrategyCommentQueryObject qo){
        qo.setOrderBy("sc.createTime desc");
        return strategyCommentService.query(qo);
    }


    //查询收藏
    @GetMapping("/{userId}/collectTraves")
    public Object queryCollect(@PathVariable("userId") Long userId,Long travelId){
        return userService.getCollectRelation(userId, travelId);
    }

    //取消收藏
    @DeleteMapping("{userId}/collectTraves")
    public Object cancelCollect(@PathVariable Long userId,Long travelId){
        userService.deleteCollect(userId,travelId);
        return new JSONResult();
    }

    //添加收藏
    @PostMapping("{userId}/collectTraves")
    public Object addCollect(@PathVariable Long userId,Long travelId){
        userService.saveCollect(userId,travelId);
        return new JSONResult();
    }




    //用户点赞
    @GetMapping
    public Object listLike(User user){

        return userService.listLike(user);
    }
    //根据userId查询当前用户的个人信息
    @GetMapping("{userId}")
    public User queryUserById(@PathVariable Long userId){
        User user=userService.queryUserById(userId);
        return user;
    }
    //查询用户用积分买了什么
    @GetMapping("{userId}/products")
    public User selectAllProducts(@PathVariable Long userId){
       return userService.selectAllProducts(userId);
    }

    //查询旅行单的列表
    @GetMapping("{userId}/travelLists")
    public PageInfo queryTravelList(TravelListQueryObject qo){
        return userService.queryTravelList(qo);
    }

    //查询有无添加该旅行单
    @GetMapping("{userId}/travelLists/{strategyId}")
    public Boolean queryIsExist(@PathVariable Long userId,@PathVariable Long strategyId){
        return userService.queryIsExist(userId,strategyId);
    }

    //添加该旅行单
    @PostMapping("{userId}/travelLists")
    public JSONResult insertTravelList(@PathVariable Long userId,Long strategyId){
        userService.saveTravelList(userId,strategyId);
        return new JSONResult();
    }

}
